package com.shadow.istio.client.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class GrpcClientCommandLineRunner implements CommandLineRunner {
    @Autowired
    GrpcClientConfiguration configuration;

    @Override
    public void run(String... args) throws Exception {
        // 开启 grpc 客户端
        configuration.start();

        // 添加关闭客户端逻辑, Runtime 获取当前jvm运行环境, 调用addShutdownHook ，
        // 当jvm结束前会执行 addShutdownHook中注册的 Thread 线程, 当然也是在jvm退出前关闭 grpc 远程连接了
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            try {
                configuration.shutdown();
            }catch (Exception e){
                e.printStackTrace();
            }
        }));

    }
}
